import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/theme/gt_colors_v3.dart';
import 'package:lib_uikit/widgets/gt_button_v3.dart';

import '../../generated/l10n.dart';

/// @Title   弹出文字列表bottomsheet
/// @Author: bryce
/// @CreateDate: 2023年10月18日16:09:54
Future<int?> showGTBottomSheetV3(
  context, {
  required Widget child, // 子组件
  double? maxHeight, // bottomSheet最大高度
  double? minHeight, // bottomSheet最小高度
  double topPadding = 0, // child上边距
  double bottomPadding = 16, // child下边距，不是指cancel按钮下的边距
  double leftPadding = 16,
  double rightPadding = 16,
  double titleLeftPadding = 0,
  double topRadius = 12.0, // bottomSheet上方圆角
  bool showBottomCancel = false, // 是否展示底部取消按钮
  bool showTopRightClose = false, // 是否展示右上角关闭按钮
  VoidCallback? topRightCloseCallBack,
  VoidCallback? dismissCallBack,
  String? title,
  double? backgroundOpacity,
}) async {
  // 最大高度
  double bottomSheetMaxHeight = maxHeight ?? (GTScreen.height * 0.75);
  double cancelHeight = showBottomCancel ? 48 : 0;
  double titleHeight = title != null ? 51 : 0;
  // child的最大高度：总的最大高度-标题高度-child上边距-child下边距-cancel高度
  double childMaxHeight = bottomSheetMaxHeight - titleHeight - topPadding - bottomPadding - cancelHeight;
  double childMinHeight = minHeight ?? 0 - titleHeight - topPadding - bottomPadding - cancelHeight;
  if (childMaxHeight < 0) childMinHeight = 40;
  if (childMinHeight < 0) childMinHeight = 0;

  Widget _closeWidget() {
    return GestureDetector(
      onTap: () {
        topRightCloseCallBack?.call();
        Navigator.pop(context);
      },
      child: Padding(
        padding: const EdgeInsetsDirectional.fromSTEB(16, 16, 16, 27),
        child: Icon(
          GTIconFont.e89b,
          color: GTColorsV3.icon3.color,
          size: 16,
        ),
      ),
    );
  }

  return showModalBottomSheet<int>(
    // constraints: BoxConstraints(maxHeight: maxHeight),
    backgroundColor: Colors.transparent,
    barrierColor: Color.fromRGBO(0, 0, 0, backgroundOpacity ?? 0.5),
    isScrollControlled: true,
    context: context,
    builder: (BuildContext context) {
      return Wrap(
        children: [
          Stack(
            children: [
              Container(
                padding: GTEdgeInsetsOnly(top: 8),
                clipBehavior: Clip.antiAlias,
                decoration: BoxDecoration(
                  color: GTColorsV3.cmpt16.color,
                  borderRadius:
                      BorderRadius.only(topLeft: Radius.circular(topRadius), topRight: Radius.circular(topRadius)),
                ),
                child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [
                  // 设置标题
                  title != null
                      ? Container(
                          padding: GTEdgeInsetsOnly(left: leftPadding + titleLeftPadding, right: 40),
                          height: titleHeight,
                          alignment: GTThemeProvider.instance.isRTL() ? Alignment.centerRight : Alignment.centerLeft,
                          child: Text(
                            title,
                            style: gtTextStyleW700(color: GTColorsV3.text1.color, fontSize: 16),
                          ))
                      : Container(),
                  // child
                  Container(
                      margin: EdgeInsets.only(
                          top: topPadding, bottom: bottomPadding, left: leftPadding, right: rightPadding),
                      constraints: BoxConstraints(maxHeight: childMaxHeight),
                      child: child),
                  // 设置Cancel按钮
                  showBottomCancel
                      ? SafeArea(
                          child: Container(
                              padding: const EdgeInsets.only(bottom: 12, left: 16, right: 16),
                              child: GTButtonV3(
                                  text: LibUIKitS.of(context).Cancel,
                                  isFullWidth: true,
                                  buttonSize: GTButtonSizeV3.medium,
                                  buttonType: GTButtonTypeV3.outlined,
                                  onPressed: () {
                                    Navigator.pop(context);
                                  })))
                      : Container()
                ]),
              ),
              // 设置右上角关闭
              showTopRightClose
                  ? GTThemeProvider.instance.isRTL()
                      ? Positioned(top: 0, left: 0, child: _closeWidget())
                      : Positioned(top: 0, right: 0, child: _closeWidget())
                  : Container()
            ],
          )
        ],
      );
    },
  ).whenComplete((){
    dismissCallBack?.call();
  });
}
